/*
 * support3
 */
package com.pitop.scene.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.SceneException;
import com.pitop.core.tools.StringTools;
import com.pitop.scene.dao.SceneDAO;
import com.pitop.scene.entity.Scene;
import com.pitop.scene.entity.SceneState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("sceneSV")
@CacheConfig(cacheNames = "Support:Scene_Cache")
@Slf4j
public class SceneSVImpl extends BaseSVImpl<Scene, Long> implements SceneSV {

    @Autowired
    private SceneDAO sceneDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return sceneDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    @CacheEvict(allEntries = true)
    public void save(Scene entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        super.save(entity);
    }

    /**
     * 数据更新
     *
     * @param scene 场景实体
     */
    @Override
    @CacheEvict(allEntries = true)
    public void modify(Scene scene) {
        getBaseDAO().update(scene);
    }

    /**
     * 加载一个对象Scene
     *
     * @param id * @param code 编码
     * @return Scene
     */
    @Override
    @Cacheable(key = "T(String).valueOf(#id).concat('-').concat(#code)")
    public Scene load(Long id, String code) {
        if (id == null && code == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return sceneDAO.load(param);
    }

    /**
     * 加载一个对象Scene 通过id
     *
     * @param id
     * @return Scene
     */
    @Override
    @Cacheable(key = "T(String).valueOf(#id)")
    public Scene loadById(Long id) {
        if (id == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return sceneDAO.loadById(id);
    }

    /**
     * 加载一个对象Scene 通过code
     *
     * @param code 编码
     * @return Scene
     */
    @Override
    @Cacheable(key = "#code")// 拼接字符串在后面跟上( .concat('-test') ),也可以直接(+'-test')
    public Scene loadByCode(String code) {
        if (code == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return sceneDAO.loadByCode(code);
    }

    /**
     * 加载一个对象Scene 通过num
     *
     * @param num 场景编号
     * @return Scene
     */
    @Override
    public Scene loadByNum(String num) {
        if(StringTools.isEmpty(num)) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> params = new HashedMap();
        params.put("num", num);
        return sceneDAO.load(params);
    }


    /**
     * 根据主键id,oldStates 共同更新 Scene 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    @CacheEvict(allEntries = true)
    public void updateStateById(Long id, SceneState newState, SceneState... oldStates) {
        if (id == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        sceneDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键code,oldStates 共同更新 Scene 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    @CacheEvict(allEntries = true)
    public void updateStateByCode(String code, SceneState newState, SceneState... oldStates) {
        if (code == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        sceneDAO.updateStateByCode(code, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 Scene 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    @CacheEvict(allEntries = true)
    public void updateById(Long id, SceneState state) {
        if (id == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        sceneDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键code 更新 Scene 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    @CacheEvict(allEntries = true)
    public void updateByCode(String code, SceneState state) {
        if (code == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        sceneDAO.updateByCode(code, state.name(), new Date());
    }


    /**
     * 删除对象Scene
     *
     * @param id * @param code 编码
     * @return Scene
     */
    @Override
    @CacheEvict(allEntries = true)
    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new SceneException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        sceneDAO.delete(param);
    }


    /**
     * 查询Scene分页
     *
     * @param scene  对象
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Scene>
     */
    @Override
    @Cacheable(key = "'list'")
    public List<Scene> list(Scene scene, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (scene != null) {
            map = JSON.parseObject(JSON.toJSONString(scene, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return sceneDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(Scene scene) {
        Map<String, Object> map = null;
        if (scene != null) {
            map = JSON.parseObject(JSON.toJSONString(scene, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return sceneDAO.count(map);
    }

    /**
     * 查询Scene分页
     *
     * @param id     * @param code  编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Scene>
     */
    @Override
    @Cacheable(key = "T(String).valueOf(#id).concat('-').concat(#code).concat('-').concat('list')")
    public List<Scene> list(Long id, String code, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return sceneDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return sceneDAO.count(param);
    }

}
